package com.zunipe.accounts;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.zunipe.accounts.database.Result;
import com.zunipe.accounts.database.entity.User;
import com.zunipe.accounts.database.handler.LoginHandler;
import com.zunipe.accounts.databinding.ActivityRegisterBinding;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
    ActivityRegisterBinding binding;
    private LoginHandler loginHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityRegisterBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        loginHandler = new LoginHandler();

        binding.btnLogin.setOnClickListener(this);
        binding.btnRegister.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btn_register) {
            String username = binding.etUsername.getText().toString();
            String password = binding.etPassword.getText().toString();

            Result result = loginHandler.register(new User(username, password));
            if (result.getCode() == Result.FAILED_CODE) {
                Toast.makeText(this, result.getMsg(), Toast.LENGTH_SHORT).show();
                return;
            }
            Toast.makeText(this, result.getMsg(), Toast.LENGTH_SHORT).show();
            finish();
        } else if (id == R.id.btn_login) {
            finish();
        }
    }
}